/**
 * describe:
 *
 * @author chaP
 * @date 2019/01/23
 */
package CodingTest.AC20190123;

public class rotate {
    /**
     * 方法一：双重循环
     * @param nums
     * @param k
     */
    public void rotate1(int[] nums, int k) {
        int len = nums.length;
        k = k%len;
        for(int i = 0;i<k;i++){
            int temp = nums[len-1];
            for(int j = len-1;j>0;j--)
                nums[j] = nums[j-1];
            nums[0] = temp;
        }



    }
    /**
     * 方法二：翻转，逆序法，经典
     */
    public void rotate2(int[] nums, int k) {
        int n = nums.length;
        k %=n;
        reverse(nums,0,n-1);
        reverse(nums,0,k-1);
        reverse(nums,k,n-1);
    }
    private void reverse(int[] nums,int start,int end){
        while(start < end){
            int temp = nums[start];
            nums[start++] = nums[end];
            nums[end--] = temp;
        }
    }

    public static void main(String[] args) {
        int[] nums = {1,2,3,4,5,6,7};
        rotate r = new rotate();
        r.rotate2(nums,3);
    }
}
